package com.crl.animation
{
	import com.crl.core.crl_internal;
	import com.crl.manager.ITickable;
	import com.crl.manager.TickManager;
	
	import flash.utils.getTimer;

	use namespace crl_internal;
	public class AnimationPlayer implements ITickable
	{
		private var _tickManager:TickManager;
		
		public var firstRender:AbstractRender;
		public var lastRender:AbstractRender;
		
		public function AnimationPlayer()
		{
		}
		
		public static var ins:AnimationPlayer;
		
		public function set tickManager(value:TickManager):void{
			_tickManager=value;
			
			_tickManager.add(this);
		}
		
		public function add(item:AbstractRender):void{
			if(item.active){
				return;
			}
			item.active=true;
			
			if(lastRender){
				lastRender.next=item;
				item.pre=lastRender;
			}else{
				firstRender=lastRender=item;
			}
		}
		
		
		public function remove(item:AbstractRender):void{
			if(item.active==false){
				return;
			}
			item.active=false;
			
			var pre:AbstractRender=item.pre;
			var next:AbstractRender=item.next;
			
			if(pre){
				pre.next=next;
			}else{
				firstRender=next;
			}
			if(next){
				next.pre=pre;
			}else{
				lastRender=pre;
			}
		}
		
		
		public function tick(time:int):void
		{
			var t:int=getTimer();
			var r:AbstractRender=firstRender;
			while(r){
				r.render(t);
				r=r.next;
			}
		}
		
	}
}